package tue.cse.advanceddatabases.project.db;

import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

public class UserManager {
	
	private final Logger logger = Logger.getLogger(UserManager.class.getName());
	
	private JPAResourceBean jpa;
	
	public UserManager(JPAResourceBean jpaResourceBean){
		jpa = jpaResourceBean;
	}
	
	public User findUserByLoginID(String loginID){
		EntityManager em = null;
		try
		{
			em = jpa.getEMF().createEntityManager();
			Query findActiveUserByLoginIdQuery = em.createNamedQuery("findActiveUserBySystemLoginID");
			findActiveUserByLoginIdQuery.setParameter("loginId", loginID);
			
			List result = findActiveUserByLoginIdQuery.getResultList();
			
			if (result == null || result.size()==0)
				return null;
			
			return (User)result.get(0);
		} finally{
			if (em != null)
				em.close();
		}
	}

	public void add(User aUser) {
		logger.info("Adding user:");
		logger.info("ID:" + aUser.getUserID());
		logger.info("LogID:" + aUser.getLoginId());
		logger.info("Name:" + aUser.getFirstName());
		EntityManager em = null;
		try {
			aUser.setSystemRole("U");
			aUser.setActive(true);
			em = jpa.getEMF().createEntityManager();
			EntityTransaction t = em.getTransaction();
	
			t.begin();
			em.persist(aUser);
			t.commit();
		} finally {
			if (em != null)
				em.close();
		}
	}
	
	//Goran: add methods 
	public User findUserByUserID(long userID){
		EntityManager em = null;
		try
		{
			em = jpa.getEMF().createEntityManager();
			Query findUserByUserIdQuery = em.createNamedQuery("findUserByUserIdQuery");
			findUserByUserIdQuery.setParameter("userId", userID);
			
			List result = findUserByUserIdQuery.getResultList();
			
			if (result == null || result.size()==0)
				return null;
			
			return (User)result.get(0);
		} finally{
			if (em != null)
				em.close();
		}
	}
	
	public List<User> getAllUsers(){
		EntityManager em = null;
		try
		{
			em = jpa.getEMF().createEntityManager();
			Query getAllUsersQuery = em.createNamedQuery("getAllUsersQuery");
						
			List result = getAllUsersQuery.getResultList();
			
			if (result == null || result.size()==0)
				return null;
			
			return parse(result);
		} finally{
			if (em != null)
				em.close();
		}
	}
	
	public List<User> getUsersBySearchCriteria(String loginID, String name, String country){
		EntityManager em = null;
		try
		{
			em = jpa.getEMF().createEntityManager();
			Query getUsersByNameLoginIDContryQuery = em.createNamedQuery("getUsersByNameLoginIDContryQuery");
			getUsersByNameLoginIDContryQuery.setParameter("name", name);
			getUsersByNameLoginIDContryQuery.setParameter("loginId", loginID);
			getUsersByNameLoginIDContryQuery.setParameter("country", country);
			
			List result = getUsersByNameLoginIDContryQuery.getResultList();
			
			if (result == null || result.size()==0)
				return null;
			
			return parse(result);
		} finally{
			if (em != null)
				em.close();
		}
	}
	
	public List<User> getUsersByLoginID(String loginID){
		EntityManager em = null;
		try
		{
			em = jpa.getEMF().createEntityManager();
			Query getUsersByLoginIDQuery = em.createNamedQuery("findActiveUserBySystemLoginID");
			getUsersByLoginIDQuery.setParameter("loginId", loginID);
			
			List result = getUsersByLoginIDQuery.getResultList();
			
			if (result == null || result.size()==0)
				return null;
			
			return parse(result);
		} finally{
			if (em != null)
				em.close();
		}
	}
	
	public List<User> parse(List usersList) {
	  	List<User> users = new Vector<User>();
		
		//logger.info("Number of Users are:" + usersList.size());
		for (int i=0; i< usersList.size();i++){
			User userElement = (User)usersList.get(i);
			users.add(userElement);
		}
		return users;
	}
}
